package com.open.capacity.user.service.impl;

import com.open.capacity.commons.Result;
import com.open.capacity.reimburse.util.uuid.UUIDUtils;
import com.open.capacity.user.dao.ExcelImportDao;
import com.open.capacity.user.dao.ExcelImportFieldsDao;
import com.open.capacity.user.model.ExcelImport;
import com.open.capacity.user.model.ExcelImportFields;
import com.open.capacity.user.service.ExcelImportService;
import com.open.capacity.utils.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Slf4j
@Service
public class ExcelImportServiceImpl implements ExcelImportService {

    @Autowired
    private ExcelImportDao importDao;

    @Autowired
    private ExcelImportFieldsDao importFieldsDao;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(ExcelImport excelImport) {
        excelImport.setCreateTime(new Date());
        excelImport.setUpdateTime(excelImport.getCreateTime());

        importDao.save(excelImport);
        log.info("保存导入配置：{}", excelImport);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(ExcelImport excelImport) {
        excelImport.setUpdateTime(new Date());

        importDao.updateByOps(excelImport);
        log.info("修改导入配置：{}", excelImport);
    }

    @Override
    public Result updateStatus(String id, Integer status) {
        ExcelImport excelImport = importDao.findById(id);
        excelImport.setStatus(status);
        importDao.updateByOps(excelImport);
        log.info("修改状态：{}", excelImport);
        return Result.succeed("修改成功");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(String id) {
        ExcelImport excelImport = importDao.findById(id);

        importDao.delete(id);

        log.info("删除导入配置：{}", excelImport);
    }

    @Override
    public ExcelImport findById(String id) {
        ExcelImport excelImport = importDao.findById(id);
        List<ExcelImportFields> fields = importFieldsDao.findByImportId(id);
        excelImport.setFields(fields);
        return excelImport;
    }

    @Override
    public ExcelImport findByCode(String code) {
        return null;
    }

    @Override
    public List<ExcelImport> findByParentId(String parentId) {
        return null;
    }

    @Override
    public List<ExcelImport> findImports(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
//        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
//            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);
        List<ExcelImport> list = importDao.findList(params);

        return list;
    }


    @Override
    public Result saveOrUpdate(ExcelImport excelImport, String keyValue) {
        int i = 0;

        if (StringUtils.isNotEmpty(keyValue)) {
            ExcelImport model = importDao.findById(keyValue);
            if (model != null) {
                excelImport.setId(keyValue);
                excelImport.setStatus(1);
                excelImport.setUpdateTime(new Date());
                i = importDao.updateByOps(excelImport);

                //删除旧数据
                importFieldsDao.deleteByImportId(keyValue);
                //重新配置
                for (ExcelImportFields field : excelImport.getFields()) {
                    field.setId(UUIDUtils.getOriginalUUIDWithoutBar());
                    field.setImportId(keyValue);
                    importFieldsDao.save(field);
                }
            }
        } else {
            String id = UUIDUtils.getOriginalUUIDWithoutBar();
            excelImport.setId(id);
            excelImport.setStatus(1);
            excelImport.setCreateTime(new Date());
            excelImport.setUpdateTime(excelImport.getCreateTime());

            for (ExcelImportFields field : excelImport.getFields()) {
                field.setId(UUIDUtils.getOriginalUUIDWithoutBar());
                field.setImportId(id);
                importFieldsDao.save(field);
            }

            i = importDao.save(excelImport);

        }

        return i > 0 ? Result.succeed("操作成功") : Result.failed("操作失败");
    }


}
